package com.myblog.controller;

import com.myblog.constant.CodeType;
import com.myblog.model.PrivateWord;
import com.myblog.model.User;
import com.myblog.service.PrivateWordService;
import com.myblog.service.UserService;
import com.myblog.utils.DataMap;
import com.myblog.utils.JsonResult;
import com.sun.org.apache.bcel.internal.classfile.Code;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Slf4j
@RestController
public class PrivateWordController {

    @Autowired
    private PrivateWordService privateWordService;

    @Autowired
    private UserService userService;


    /**
     * 后台查看悄悄话
     * @param request
     * @return
     */
    @PostMapping("/getAllPrivateWord")
    public String getAllPrivateWord(HttpServletRequest request){
        try{
            //获取用户id,判断是否登录
            Principal userPrincipal = request.getUserPrincipal();
            if (userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            User user = userService.searchUserByName(userPrincipal.getName());
            //调用获取悄悄话的service方法
            DataMap data = privateWordService.getAllPrivateWord();
            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("PrivateWordController getAllPrivateWord Exception",e);
        }
        return JsonResult.fail().toJSON();
    }

    /**
     * 个人主页查看悄悄话
     * @param rows
     * @param pageNum
     * @param request
     * @return
     */
    @PostMapping("/getPrivateWordByPublisher")
    public String getPrivateWord(@Param(value = "rows")Integer rows, @Param(value = "pageNum")Integer pageNum,
                                 HttpServletRequest request){
        try{
            //获取用户id
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            User user = userService.searchUserByName(userPrincipal.getName());
            //调用获取悄悄话的service方法
            DataMap data = privateWordService.getAllPrivateWordByUserId(rows,pageNum,user.getId());
            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("PrivateWordController getPrivateWordByPublisher Exception",e);
        }
        return JsonResult.fail().toJSON();
    }

    //sendPrivateWord

    /**
     * 发送悄悄话
     * @param privateWord
     * @param request
     * @return
     */
    @PostMapping("/sendPrivateWord")
    public String sendPrivateWord(PrivateWord privateWord,HttpServletRequest request){
        try{
            //获取当前登录用户
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            User user = userService.searchUserByName(userPrincipal.getName());
            //判断用户是否登录


            DataMap data = privateWordService.sendPrivateWord(privateWord,user.getId());

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("PrivateWordController sendPrivateWord Exception",e);
        }
        return JsonResult.fail().toJSON();
    }

    //getAllPrivateWord
}
